package com.rxyl.love.petdoctor.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.rxyl.love.petdoctor.R;


/**
 * Created by Administrator on 2016/1/9 0009.
 */
public class CircleFragment extends Fragment implements RadioGroup.OnCheckedChangeListener{


    private RadioGroup radioGroup;
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.circle_page_head,container,false);

        radioGroup = (RadioGroup) view.findViewById(R.id.rg_circle);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {

        mManager = getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(this);

        mTransaction=mManager.beginTransaction();
        mTransaction.replace(R.id.frame_circle,new CircleDetailFragment()).commit();

        RadioButton rb = (RadioButton) radioGroup.getChildAt(0);
        rb.performClick();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        mTransaction = mManager.beginTransaction();
        switch (checkedId){
            case R.id.rb_circle_circle:
                mTransaction.replace(R.id.frame_circle,new CircleDetailFragment()).commit();
                break;
            case R.id.rb_topic:
                mTransaction.replace(R.id.frame_circle,new TopicFragment()).commit();
                break;
        }


    }

//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        Field childFragmentManager = null;
//
//        try {
//            childFragmentManager = Fragment.class
//                    .getDeclaredField("mChildFragmentManager");
//            childFragmentManager.setAccessible(true);
//            try {
//                childFragmentManager.set(this, null);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        }
//
//
//    }
}
